package control;

import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.bettertogether.R;

public class Contact_ExpandableListAdapter extends BaseExpandableListAdapter {

	private Context _context;
	public ImageLoader imageLoader;
	private static LayoutInflater inflater = null;
	private Activity activity;
	private String[] _listDataHeader; // header titles
	// child data in format of header title, child title
	private HashMap<String, List<Person>> _listDataChild;

	public Contact_ExpandableListAdapter(Activity context,
			String[] listDataHeader, HashMap<String, List<Person>> listChildData) {
		activity = context;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(activity.getApplicationContext());
		this._context = context;
		this._listDataChild = listChildData;
		this._listDataHeader = listDataHeader;
	}

	@Override
	public Object getChild(int groupPosition, int childPosititon) {
		return this._listDataChild.get(this._listDataHeader[groupPosition])
				.get(childPosititon);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) this._context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.tablerow, null);
		}
		Person person = (Person) getChild(groupPosition, childPosition);

		TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
		TextView txtDesc = (TextView) convertView.findViewById(R.id.desc);
		ImageView img = (ImageView) convertView.findViewById(R.id.icon);

		txtTitle.setText(person.getName() + " " + person.getSurname());
		txtDesc.setText(person.getMobilePhone());
		// img.setImageResource(R.drawable.ornekresim);
		 imageLoader.DisplayImage(person.getImg(),img);
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return this._listDataChild.get(this._listDataHeader[groupPosition])
				.size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return this._listDataHeader[groupPosition];
	}

	@Override
	public int getGroupCount() {
		return this._listDataHeader.length;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		String headerTitle = (String) getGroup(groupPosition);
		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) this._context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.message_group, null);
		}

		TextView lblListHeader = (TextView) convertView
				.findViewById(R.id.lblListHeader);
		lblListHeader.setTypeface(null, Typeface.BOLD);
		lblListHeader.setText(headerTitle);

		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
}